package org.starapp.rbac.entity;

import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Column;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableList;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author wangqiang@cisoft.com.cn
 *
 */
@Entity
@Table(name = "rbac_users")
public class Users implements Serializable{
		
				
		/**
		 * USER_ID
		 */
		private String userId;
				
		/**
		 * USER_NAME
		 */
		private String userName;
				
		/**
		 * PASSWORD
		 */
		private String password;
				
		/**
		 * SALT
		 */
		private String salt;
				
		/**
		 * REGISTER_DATE
		 */
		private Date registerDate;
		
		private  String plainPassword;
		
		
		@Transient
		@JsonIgnore
		public String getPlainPassword() {
			return plainPassword;
		}

		public void setPlainPassword(String plainPassword) {
			this.plainPassword = plainPassword;
		}


		/**
		 * Departments
		 */
		private Departments departments;
		

		private List<Departments> departmentsM2MList;				

		private List<Roles> rolesM2MList;				
		
				
		@Id
		@Column(name="USER_ID")
		@NotBlank
		public String getUserId(){
			return this.userId;
		}
		
		public void setUserId(String userId){
			this.userId = userId;
		}
		
				
		public String getUserName(){
			return this.userName;
		}
		
		public void setUserName(String userName){
			this.userName = userName;
		}
		
				
		public String getPassword(){
			return this.password;
		}
		
		public void setPassword(String password){
			this.password = password;
		}
		
				
		public String getSalt(){
			return this.salt;
		}
		
		public void setSalt(String salt){
			this.salt = salt;
		}
		
				
		@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
		@Temporal(TemporalType.TIMESTAMP)
		public Date getRegisterDate(){
			return this.registerDate;
		}
		
		public void setRegisterDate(Date registerDate){
			this.registerDate = registerDate;
		}
		
		
				
		@ManyToMany
		@JoinTable(name = "rbac_departments_users", joinColumns={@JoinColumn(name="USER_ID") },inverseJoinColumns={@JoinColumn(name="DEPARTMENT_ID")})		
		public List<Departments> getDepartmentsM2MList(){
			return this.departmentsM2MList;
		}
		
		public void setDepartmentsM2MList(List<Departments> departmentsM2MList){
			this.departmentsM2MList = departmentsM2MList;
		}
				
		@ManyToMany
		@JoinTable(name = "rbac_roles_users", joinColumns={@JoinColumn(name="USER_ID") },inverseJoinColumns={@JoinColumn(name="ROLE_ID")})		
		@JsonIgnore
		public List<Roles> getRolesM2MList(){
			return this.rolesM2MList;
		}
		
		public void setRolesM2MList(List<Roles> rolesM2MList){
			this.rolesM2MList = rolesM2MList;
		}
					
		@ManyToOne
		@JoinColumn(name = "DEPARTMENT_ID")
		public Departments getDepartments(){
			return this.departments;
		}
		
		public void setDepartments(Departments departments){
			this.departments = departments;
		}

		
		@Override
		public String toString() {
			return ToStringBuilder.reflectionToString(this);
		}
	
	}

